<!--
 * @Descripttion: 
 * @Author: liZhaoBo
 * @Date: 2024-05-10 15:07:17
 * @LastEditors: liZhaoBo
 * @LastEditTime: 2024-05-10 15:18:29
-->
<template>
  <a-modal
    v-model:visible="visible"
    title="批量编辑"
    width="500px"
    title-align="start"
    align-center
    render-to-body
    :mask-closable="false"
    class="dmo-common-dialog"
    @close="closed"
  >
    <div class="secret-level-ara">
      <a-form
        ref="secretFormRef"
        :rules="rulesChild"
        :disabled="false"
        label-align="left"
        :model="secretForm"
      >
        <a-form-item label="任务编号" field="taskNum">
          <a-input
            v-model="secretForm.taskNum"
            :disabled="props.rowData ? true : false"
            placeholder="请输入"
            allow-clear
          />
        </a-form-item>
        <a-form-item label="任务名称" field="taskName">
          <a-input
            v-model="secretForm.taskName"
            placeholder="请输入"
            allow-clear
          />
        </a-form-item>
      </a-form>
    </div>

    <template #footer>
      <a-button @click="closed">{{ '取消' }}</a-button>
      <a-button type="primary" :disabled="importLoading" @click="save">{{
        '确认'
      }}</a-button>
    </template>
  </a-modal>
</template>

<script setup lang="ts">
  import { ref, defineProps, onMounted, defineEmits } from 'vue';
  import {
    saveOrderTask,
    editOrderTask,
    getOrderTaskInfo,
  } from '@/api/purchase-order/taskNumberSafeguard';

  const emit = defineEmits(['closeDrawer', 'success']);
  const props = defineProps({
    rowData: {
      type: Object,
      default: null,
    },
  });
  const secretFormRef = ref();
  const secretForm = ref({
    taskNum: '',
    taskName: '',
  });
  const importLoading = ref<boolean>(false);
  const visible = ref(true);
  const rulesChild = ref({
    taskNum: [
      {
        required: true,
        message: '请输入',
        trigger: ['blur', 'change'],
      },
    ],
    taskName: [
      {
        required: true,
        message: '请输入',
        trigger: ['blur', 'change'],
      },
    ],
  });

  onMounted(async () => {
    if (props.rowData) {
      const res: any = await getOrderTaskInfo(props.rowData.id);
      if (res.code === 200) {
        secretForm.value = res.data;
      }
    }
  });
  const save = async () => {
    const result = await secretFormRef.value.validate();
    if (!result) {
      const api = props.rowData ? editOrderTask : saveOrderTask;
      const res: any = await api(secretForm.value);
      if (res.code === 200) {
        emit('success');
      }
    }
  };
  const closed = () => {
    emit('closeDrawer');
  };
</script>
